package com.qf.factories;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;

import java.io.InputStream;

public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory> {

    /**
     * 返回的是当前FactoryBean想要封装的对象
     * @return
     * @throws Exception
     */
    @Override
    public SqlSessionFactory getObject() throws Exception {

        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        return factory;
    }

    /**
     * 返回的是 当前类中 getObject方法返回的对象的类型
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return SqlSessionFactory.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
